var express = require('express');
var router = express.Router();
//为什么没有？
var mysql = require('mysql');

var config = require('../conf/mysql');
// var $sql = require('./userSql');

var pool = mysql.createPool(config);

module.exports = {

    //增加
    add: function (req, res,callback) {
        var flags=0;
        pool.getConnection(function(err, connection) {
            connection.query(req, function(err, result) {
                if(err) {
                    // res.send(err); 
                    console.log("err")
                }else{
                    flags=1;
                    // res.send('add success');
                    console.log("add sucess")
                }
                // 释放连接 
                connection.release();
            });
        });
        setTimeout(function(){
            callback(flags)
        },1000);
        // pool.getConnection(function(err, connection) {
        //     var param = req.query || req.params;
        //     connection.query($sql.insert, [param.name, param.age], function(err, result) {
        //         if(err) {
        //             res.send(err); 
        //         }else{
        //             res.send('add success');
        //         }
        //         // 释放连接 
        //         connection.release();
        //     });
        // });
    },

    //查询
    queryAll: function (req, res,callback) {
        var check_result
        pool.getConnection(function(err, connection) {
            console.log(req)
            // connection.query($sql.queryAll, function(err, result) {
            connection.query(req, function(err, result) {
                // if(err) {
                //     res.send(err); 
                // }else{
                //     res.send(result);
                // }
                // console.log("req",req);
                // next=result;
                check_result=result;
                // console.log("2",check_result);
                connection.release();
                
            });
        });
        
        setTimeout(function(){
            // console.log("3",check_result);
            callback(check_result)
            // return check_result;
        },1000);
    },

    //修改
    update: function (req, res, next) {
        pool.getConnection(function(err, connection) {
            var param = req.query || req.params;
            
            connection.query($sql.update, [param.name, param.age, +param.id], function(err, result) {
                if(err) {
                    res.send(err); 
                }else{
                    res.send('update success');
                }

                connection.release();
            });
        });
    },


    //删除
    delete: function (req, res, next) {
        pool.getConnection(function(err, connection) {
            var id = +req.query.id;
            connection.query($sql.delete, id, function(err, result) {
                if(err) {
                    res.send(err); 
                }else{
                    res.send('delete success');
                }
                connection.release();
            });
        });
    }
};
